package com.xywy.common.manager;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.util.StringUtils;
import com.xywy.wjk.AddDevicesActivity;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by xgd on 2014/6/12.
 */
public class TitleManager implements Observer, AdapterView.OnItemClickListener {

	private static String TAG = "TitleManager";
	private TextView ivAdd;

	private TitleManager() {
	}

	public static TitleManager getInstance() {
		return SingletonHolder.instance;
	}

	public static class SingletonHolder {
		private static TitleManager instance = new TitleManager();
	}

	protected BaseActivity mActivity;

	// ****标题栏****//
	private RelativeLayout title_wjk;
	private LinearLayout title_jtys;
	private RelativeLayout title_find;
	private RelativeLayout title_fwc;
	private RelativeLayout title_me;

	// 填充器
	private LayoutInflater inflater;

	public void init(BaseActivity activity) {

		this.mActivity = activity;
		// 获取填充对象
		inflater = LayoutInflater.from(mActivity);

		initView();
	}

	private void initView() {

		title_wjk = (RelativeLayout) mActivity
				.findViewById(R.id.rl_main_title_wjk);
		title_jtys = (LinearLayout) mActivity
				.findViewById(R.id.rl_main_title_jtys);
		title_find = (RelativeLayout) mActivity
				.findViewById(R.id.rl_main_title_find);
		title_fwc = (RelativeLayout) mActivity
				.findViewById(R.id.rl_main_title_fwc);
		ivAdd = (TextView) mActivity.findViewById(R.id.iv_wjk_right_add);
		ivAdd.setOnClickListener(addOnClickListener);
	}

	private int id;

	@Override
	public void update(Observable observable, Object data) {
		// 控制标题栏变动
		if (data != null && !StringUtils.isNull(data.toString())) {
			id = Integer.parseInt(data.toString());

			switch (id) {
			case ConstantValue.FG_WJK:
				showwjkTitle();
				break;
			case ConstantValue.FG_FIND:
				showFindTitle();
				break;
			case ConstantValue.FG_ME:
				break;
			case ConstantValue.FG_FWC:
				showWysTitle();
				break;
			}
		}
	}

	/**
	 * 添加按钮 OnClickListener
	 */
	View.OnClickListener addOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			Intent intent = new Intent(mActivity, AddDevicesActivity.class);
			mActivity.startActivity(intent);
		}
	};

	/**
	 * 分别展示导航
	 */
	/**
	 * 微健康
	 */
	private void showwjkTitle() {
		clearTitle();
		title_wjk.setVisibility(View.VISIBLE);
	}

	/**
	 * 家庭医生
	 */
	private void showJTYSTitle() {
		clearTitle();
		title_jtys.setVisibility(View.VISIBLE);
	}

	/**
	 * 发现
	 */
	private void showFindTitle() {
		clearTitle();
		title_find.setVisibility(View.VISIBLE);
	}

	/**
	 * 服务窗口的标题
	 */
	private void showWysTitle() {
		clearTitle();
		title_fwc.setVisibility(View.VISIBLE);

	}

	// /**
	// * 我
	// */
	// private void showMeTitle() {
	// clearTitle();
	// title_me.setVisibility(View.VISIBLE);
	// }

	private void clearTitle() {
		title_wjk.setVisibility(View.GONE);
		title_jtys.setVisibility(View.GONE);
		title_find.setVisibility(View.GONE);
		title_fwc.setVisibility(View.GONE);
		// title_me.setVisibility(View.GONE);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

	}
}
